/****************************************************************************
*
*  ALi (Shanghai) Corporation, All Rights Reserved. 2003 Copyright (C)
*
*  File: win_aliplay_pairing_con.c
*
*  Description: 
*              
*  History:
*      Date        	Author         	Version     	Comment
*      ====        	======        	=======   	=======
*
****************************************************************************/
#include <sys_config.h>
#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#ifndef NEW_DEMO_FRAME
#include <api/libpub29/lib_pub27.h>
#include <api/libpub29/lib_as.h>
#endif
#include <hld/decv/decv.h>
#include <api/libosd/osd_lib.h>
#include "osdobjs_def.h"
#include "string.id"
#include "images.id"
#include "osd_config.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_signalstatus.h"

#ifdef SUPPORT_CAS_A
#include "win_com_menu_define.h"
#endif

#include "aliplay_service.h"

/*******************************************************************************
* Objects declaration
*******************************************************************************/
extern CONTAINER win_aliplay_pairing_con;

extern MULTI_TEXT aliplay_pairing_mtxt;
extern TEXT_FIELD aliplay_pairing_btn_ok;
extern TEXT_FIELD aliplay_pairing_btn_cancel;
extern TEXT_FIELD 	ys_txt_msg;


static VACTION aliplay_pairing_btn_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT aliplay_pairing_btn_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION aliplay_pairing_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT aliplay_pairing_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


#define WIN_SH_IDX	  	WSTL_WIN_BODYRIGHT_01_HD
#define MTXT_SH_IDX		WSTL_TEXT_09_HD

#define BTN_SH_IDX   WSTL_BUTTON_SHOR_HD
#define BTN_HL_IDX   WSTL_BUTTON_SHOR_HI_HD
#define BTN_SL_IDX   WSTL_BUTTON_01_FG_HD
#define BTN_GRY_IDX  WSTL_BUTTON_SHOR_GRY_HD

#define MSG_SH_IDX WSTL_TEXT_09_HD

#ifndef SD_UI
#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	248
#define	W_T     	98
#define	W_W     	692
#define	W_H     	488
#endif


#define CON_L		W_L
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 46)
#define CON_H		40
#define CON_GAP		12


#define MTXT_L		(W_L + 300)
#define MTXT_T 		(W_T + 200)
#define MTXT_W 		60//(W_W - 60)
#define MTXT_H 		40

#define BTN_L1		(W_L + 120)
#define BTN_L2		(BTN_L1 + BTN_W + 40)
#define BTN_T		(MTXT_T + MTXT_H + 30)
#define BTN_W		160
#define BTN_H		40
#else
#define	W_L     	105//384
#define	W_T     	57//138
#define	W_W     	482
#define	W_H     	370

#define CON_L		W_L
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 26)
#define CON_H		30
#define CON_GAP		8


#define MTXT_L		(W_L + 20)
#define MTXT_T 		(W_T + 30)
#define MTXT_W 		(W_W - 60)
#define MTXT_H 		150

#define BTN_L1		(W_L + 60)
#define BTN_L2		(BTN_L1 + BTN_W + 40)
#define BTN_T		(MTXT_T + MTXT_H + 30)
#define BTN_W		100
#define BTN_H		32

#endif

#define LDEF_MTXT(root,varMtxt,nextObj,l,t,w,h,cnt,content)	\
	DEF_MULTITEXT(varMtxt,&root,nextObj,C_ATTR_ACTIVE,0, \
    	0,0,0,0,0, l,t,w,h, MTXT_SH_IDX,MTXT_SH_IDX,MTXT_SH_IDX,MTXT_SH_IDX,   \
	    NULL,NULL,  \
    	C_ALIGN_LEFT | C_ALIGN_TOP, cnt,  4,4,w-8,h-8,NULL,content)

#define LDEF_TXT_BTN(root,varTxt,nextObj,l,t,w,h,ID,IDl,IDr,resID)		\
    DEF_TEXTFIELD(varTxt,&root,nextObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,ID,ID, l,t,w,h, BTN_SH_IDX,BTN_HL_IDX,BTN_SL_IDX,BTN_GRY_IDX,   \
    aliplay_pairing_btn_keymap,aliplay_pairing_btn_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 10,0,resID,NULL)

#define LDEF_TXT_MSG(root,varTxt)		\
    DEF_TEXTFIELD(varTxt,&root,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0,  0,0,0,0, MSG_SH_IDX,0,0,0,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0,display_strs[21])

#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
    aliplay_pairing_keymap,aliplay_pairing_callback,  \
    nxtObj, focusID,0)


extern TEXT_CONTENT aliplay_pairing_mtxt_content[];
#define MTXT_CTN_CNT  1//(sizeof(aliplay_pairing_mtxt_content)/sizeof(TEXT_CONTENT))

LDEF_MTXT(win_aliplay_pairing_con,aliplay_pairing_mtxt,/*&aliplay_pairing_btn_ok*/NULL,\
		MTXT_L, MTXT_T,MTXT_W,MTXT_H,MTXT_CTN_CNT,aliplay_pairing_mtxt_content)

LDEF_TXT_BTN(win_aliplay_pairing_con,aliplay_pairing_btn_ok,&aliplay_pairing_btn_cancel,
		BTN_L1, BTN_T, BTN_W, BTN_H,1,2,2,RS_COMMON_YES)

LDEF_TXT_BTN(win_aliplay_pairing_con,aliplay_pairing_btn_cancel,NULL,
		BTN_L2, BTN_T, BTN_W, BTN_H,2,1,1,RS_COMMON_NO)
	
//LDEF_WIN(win_aliplay_pairing_con,&aliplay_pairing_mtxt,W_L, W_T,W_W,W_H,2)
LDEF_TXT_MSG(win_aliplay_pairing_con,ys_txt_msg)
LDEF_WIN(win_aliplay_pairing_con, &ys_txt_msg, W_L, W_T, W_W, W_H, 0)

#define BTN_YES_ID	1
#define BTN_NO_ID	2

/*******************************************************************************
*	Local functions & variables declare
*******************************************************************************/

TEXT_CONTENT aliplay_pairing_mtxt_content[] = 
{
    {STRING_ID, RS_MSG_THIS_OPERATION_WILL_ERASE_ALL},
};
/*******************************************************************************
*	Window's keymap, proc and  callback
*******************************************************************************/

static const char *clear_msg = "\0";
static char msg_buf[64] = {0};

static void win_show_msg(const char *msg, OSD_RECT *new_rect, OSD_RECT *old_rect)
{
	STRCPY(msg_buf, msg);

	if (NULL != old_rect) {
		OSD_SetRect2(&ys_txt_msg.head.frame, old_rect);
		OSD_SetTextFieldContent(&ys_txt_msg, STRING_ANSI, (UINT32)clear_msg);
		OSD_DrawObject((POBJECT_HEAD)&ys_txt_msg, C_UPDATE_ALL);
	}

	OSD_SetRect2(&ys_txt_msg.head.frame, new_rect);
	OSD_SetTextFieldContent(&ys_txt_msg, STRING_ANSI, (UINT32)msg_buf);
	OSD_DrawObject((POBJECT_HEAD)&ys_txt_msg, C_UPDATE_ALL);
}

static void win_factory_msg_update(const char *msg)
{
	UINT32 i,j;
	OSD_RECT rect;

	//ComAscStr2Uni(str, display_strs[10+usbupg_msg_line%USBUPG_MSG_MAX_LINE]);
	STRCPY(msg_buf, (char *)msg);
	
	//draw msg infos
//	for(i=0; i<NETUPG_MSG_MAX_LINE; i++)
	{
		rect.uLeft 	= MTXT_L;//MSG_L;
		rect.uTop  	= MTXT_T;//MSG_T;//+(MSG_H+MSG_GAP);
		rect.uWidth = MTXT_W;//MSG_W;
		rect.uHeight= MTXT_H;//MSG_H;

		OSD_SetRect2(&ys_txt_msg.head.frame, &rect);
		OSD_SetTextFieldContent(&ys_txt_msg, STRING_ANSI, (UINT32)msg_buf);
		OSD_DrawObject((POBJECT_HEAD)&ys_txt_msg, C_UPDATE_ALL);
	}
}

static void win_factory_msg_clear()
{
	UINT8 i;
	OSD_RECT rect;
	
//	for(i=0;i<NETUPG_MSG_MAX_LINE;i++)
	{
		msg_buf[0] = 0;

		rect.uLeft 	= MTXT_L;//MSG_L;
		rect.uTop  	= MTXT_T;//MSG_T+(MSG_H+MSG_GAP)*i;
		rect.uWidth = MTXT_W;//MSG_W;
		rect.uHeight= MTXT_H;//MSG_H;

		OSD_SetRect2(&ys_txt_msg.head.frame, &rect);
		OSD_SetTextFieldContent(&ys_txt_msg, STRING_ANSI, (UINT32)msg_buf);
		OSD_DrawObject( (POBJECT_HEAD)&ys_txt_msg, C_UPDATE_ALL);
	}

	//	netupg_msg_line = 0;	
}


static VACTION aliplay_pairing_btn_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;

	if(key == V_KEY_ENTER)
		act = VACT_ENTER;

	return act;
}

static PRESULT aliplay_pairing_btn_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;	
	//PRESULT ret = PROC_LEAVE;	
	VACTION unact;
	UINT8 bID,back_saved;
	win_popup_choice_t choice;
	UINT32 chunk_id, default_db_addr, default_db_len;

	bID = OSD_GetObjID(pObj);
	
	switch(event)
	{
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16);
		if(unact == VACT_ENTER)
		{
			ret = PROC_LEAVE;
			if(bID == BTN_NO_ID)
			{
				break;
			}
      
#ifdef SUPPORT_CAS9
#ifdef MULTI_DESCRAMBLE
			UINT8 i = 0;
			for(i = 0;i < 3;i++)  //stop filter all the TP
			{
				api_mcas_stop_transponder_multi_des(i);
			}
#else
			api_mcas_stop_transponder();
#endif
#endif
#if (defined(SUPPORT_VMX) ||defined(SUPPORT_VMX_STD))
			api_mcas_stop_transponder();
#endif

			//win_compopup_init(WIN_POPUP_TYPE_OKNO);
			//win_compopup_set_msg(NULL, NULL, RS_DISPLAY_SURE_TO_DELETE);
			//win_compopup_set_default_choice(WIN_POP_CHOICE_NO);
			//choice = win_compopup_open_ext(&back_saved);
			//if(choice == WIN_POP_CHOICE_YES)
			{
				win_compopup_init(WIN_POPUP_TYPE_SMSG);
				win_compopup_set_msg(NULL,NULL, RS_MSG_SAVING_DATA);
				win_compopup_open_ext(&back_saved);

				chunk_id = 0x03FC0100;
				api_get_chunk_add_len(chunk_id, &default_db_addr, &default_db_len);
				//here may reset CI+ Service Shunning parameter, 20100803
#ifdef CI_PLUS_SERVICESHUNNING
				ci_service_shunning_reset();
#endif
				set_default_value(DEFAULT_ALL_PROG,default_db_addr + 16);
				//init_default_value();
                
				sys_data_factroy_init(); 
				sys_data_set_factory_reset(FALSE);
				sys_data_set_cur_chan_mode(TV_CHAN);
				sys_data_save(TRUE);
				sys_data_load();
				if(GetMuteState())
					SetMuteOnOff(FALSE);
				if(GetPauseState())
					SetPauseOnOff(FALSE);
				win_compopup_smsg_restoreback();
#ifdef BIDIRECTIONAL_OSD_STYLE
				OSD_ClearScreen();
				OSD_DrawObject((POBJECT_HEAD)&g_win_mainmenu, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
#endif
				//ap_send_msg(CTRL_MSG_SUBTYPE_CMD_EXIT_ROOT, (UINT32)&g_win_PALNTSC,FALSE);
			}
#ifdef _INVW_JUICE
			inview_factory_reset();     
#endif

#ifdef SHOW_WELCOME_SCREEN				
			POBJECT_HEAD submenu;
//modify for the title of language submenu show welcome 2011 11 24
//welcome page has been solve by other method, not use the language submenu anymore
			//extern void set_win_lang_as_welcome(BOOL iswelcome);
			//set_win_lang_as_welcome(TRUE);
			submenu = (POBJECT_HEAD) & win_lan_con;
			if (OSD_ObjOpen(submenu, 0xFFFFFFFF) != PROC_LEAVE)
			{
				menu_stack_pop();
				menu_stack_push(submenu);
				ret = PROC_LOOP;
			}
							
#endif
		
		
		}
		break;
	}

	return ret;
}

static VACTION aliplay_pairing_keymap(POBJECT_HEAD pObj, UINT32 vkey)
{
	VACTION act = VACT_PASS;
	switch(vkey)
	{
	case V_KEY_LEFT:
		act = VACT_CURSOR_LEFT;
		break;
	case V_KEY_RIGHT:
		act = VACT_CURSOR_RIGHT;
		break;
	case V_KEY_MENU:
	case V_KEY_EXIT:
		act= VACT_CLOSE;
	}

	return act;
}

static char pairing_code[5] = {0};
static OSD_RECT s_old_rect;

static PRESULT aliplay_pairing_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	CONTAINER* cont = (CONTAINER*)pObj;
	UINT8	id = OSD_GetObjID(pObj);

	OSD_RECT rect;

	rect.uWidth 	= MTXT_W;
	rect.uHeight	= MTXT_H;
			
	switch(event)
	{
	case EVN_PRE_OPEN:
		OSD_SetContainerFocus(cont, BTN_NO_ID);
		//OSD_SetContainerFocus(cont, 2);
		wincom_open_title((POBJECT_HEAD)&win_aliplay_pairing_con, RS_ALIPLAY_PAIR, 0);
		break;
			
	case EVN_POST_OPEN:
		break;

	case EVN_PRE_DRAW:
		rect.uLeft 	= MTXT_L;
		rect.uTop  	= MTXT_T;
	
		aliplay_service_pairing_enable(pairing_code);
		win_show_msg(pairing_code, &rect, NULL);

		memcpy(&s_old_rect, &rect, sizeof (s_old_rect));
		
		break;

	case EVN_POST_DRAW:

		break;
	case EVN_PRE_CLOSE:
		aliplay_service_pairing_disable();
			
		/* Make OSD not flickering */
		*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
			
		break;

	case EVN_POST_CLOSE:
		break;

	case EVN_MSG_GOT:
		if (CTRL_MSG_SUBTYPE_CMD_ALIPLAY_MOVE_CURSOR == param1) {
			int x = (param2 >> 16) & 0xffff;
			int y = param2 & 0xffff;

			rect.uLeft = (W_L + 15) + (W_W - MTXT_W - 30) * ((float)x / 10000);
			rect.uTop = W_T + (W_H - MTXT_H - 15) * ((float)y / 10000);
			
			win_show_msg(pairing_code, &rect, &s_old_rect);

			memcpy(&s_old_rect, &rect, sizeof (s_old_rect));
		}
		break;
	}
	return ret;
}
